//
//  U9MatchManager.m
//  U9WatchGame
//
//  Created by uuu9 on 2017/5/19.
//  Copyright © 2017年 UUU9. All rights reserved.
//

#import "U9MatchManager.h"
#import "Match.h"
#import "U9MatchListModel.h"
#import "PubgLiveModel.h"
#import "U9NewsChannelModel.h"
#import "U9MatchDetailModel.h"

@implementation U9MatchManager

+ (void)getMatchesListWithTheme:(NSString *)theme andCid:(NSInteger)cid andTime:(NSString *)time andRType:(NSString *)rType andCompletionHandle:(void(^)(U9MatchListModel *model, NSError *error))completionHandle{
    if ([theme isEqualToString:@"推荐"]) {
        theme = @"";
    }
    NSString *encodingTheme = [theme stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *str = [NSString stringWithFormat:@"%@%@?cid=%ld&lastdate=%@&t=%@&k=%@",Api_URL,kMatchesInfo,(long)cid,time,rType,encodingTheme];
    PlistStorage *plist = [PlistStorage sharedPlistStorage];
    [self POST:str
    parameters:nil
completionHandler:^(id responseObj, NSError *error) {
    U9MatchListModel *model = [U9MatchListModel mj_objectWithKeyValues:responseObj];
    if (model.error_code != 0) {
        error = [self createError:model.error_msg];
        model = [U9MatchListModel mj_objectWithKeyValues:[plist readFromPlist:@"MatchesList"]];
    } else if (error) {
        model = [U9MatchListModel mj_objectWithKeyValues:[plist readFromPlist:@"MatchesList"]];
    } else {
        [plist storageToPlist:responseObj andFileName:@"MatchesList"];
    }
    completionHandle(model,error);
}];
    
}

+ (void)getMatcheDetailsInfoWithUserid:(NSString *)uid andSessionid:(NSString *)sessionid andMatchID:(NSString *)matchID andCompletionHandle:(void(^)(U9MatchDetailModel *model, NSError *error))completionHandle{
    [self POSTBase64:[NSString stringWithFormat:@"%@%@?id=%@",Api_URL,kNewMatchDetailsInfo,matchID]
    parameters:[NSString stringWithFormat:@"userid=%@&sessionid=%@",uid,sessionid]
completionHandler:^(id responseObj, NSError *error) {
    U9MatchDetailModel *model = [U9MatchDetailModel mj_objectWithKeyValues:responseObj];
    completionHandle(model,error);
    
}];
    
}



+ (void)getPubgMatchLiveWithID:(NSString *)matchID andCompletionHandle:(void(^)(U9NewsChannelModel *model, NSError *error))completionHandle{
    NSString *str = [NSString stringWithFormat:@"%@%@?room_id=%@",Api_URL,kPubgMatchLive,matchID];
    [self POST:str
    parameters:nil
completionHandler:^(id responseObj, NSError *error) {
    U9NewsChannelModel *model = [U9NewsChannelModel mj_objectWithKeyValues:responseObj];
    completionHandle(model,error);
}];
    
}

@end
